/*
 * ChannelInterface.cpp
 *
 */

#include "ChannelInterface.h"
#include<unistd.h>

namespace channel {


const std::string ChannelInterface::CHANNEL_INTERFACE_PATH="/tmp/agent_queue_";
const char ChannelInterface::CHANNEL_INTERFACE_RECEIVE_KEY;
const char ChannelInterface::CHANNEL_INTERFACE_SEND_KEY;

ChannelInterface::ChannelInterface(InterfaceType tipo,std::string hostName):
		m_queue(ipc::MessageQueue<channel::ChannelMessage>::get(
				ChannelInterface::CHANNEL_INTERFACE_PATH+hostName,
				(tipo == SENDER)?ChannelInterface::CHANNEL_INTERFACE_SEND_KEY:
						         ChannelInterface::CHANNEL_INTERFACE_RECEIVE_KEY)),
		 m_hostName(hostName)

{
}

ChannelInterface::~ChannelInterface()
{
}

void ChannelInterface::send(int destino,char *payload ,size_t size)
{
	channel::ChannelMessage message;

	if(size <= 0 || size > TAM_PDU_CANAL)
		throw Exception("Error en el send, el size no puede ser menor a 0 o mayor"
					    "al tamaño del PDU");

	message.destino =destino;
	memcpy(&message.payload,payload,size);
	message.size =size;

	m_queue.put(1, message);
}

void ChannelInterface::getFromQueue(channel::ChannelMessage &message)
{
	message= m_queue.get(1);
}

void ChannelInterface::recv(std::string receiver,char *payload ,size_t size)
{
	channel::ChannelMessage message;

	if(size <= 0|| size > TAM_PDU_CANAL)
		throw Exception("Error en el recv, el size no puede ser menor a 0 o mayor"
			    		"al tamaño del PDU");

	message = m_queue.get(1);

	memcpy(&payload,message.payload, message.size);
}

void ChannelInterface::recv(int mType,char *payload ,size_t size)
{
	channel::ChannelMessage message;

	if(size <= 0|| size > TAM_PDU_CANAL)
		throw Exception("Error en el recv, el size no puede ser menor a 0 o mayor"
			    		"al tamaño del PDU");

	message = m_queue.get(mType);

	memcpy(payload,message.payload, message.size);
}

void ChannelInterface::putInQueue(channel::ChannelMessage &message)
{
	m_queue.put(message.destino,message);
}

void ChannelInterface::create(std::string hostName)
{
	ipc::MessageQueue<channel::ChannelMessage>::create(ChannelInterface::CHANNEL_INTERFACE_PATH+hostName,
													   ChannelInterface::CHANNEL_INTERFACE_SEND_KEY);
	ipc::MessageQueue<channel::ChannelMessage>::create(ChannelInterface::CHANNEL_INTERFACE_PATH+hostName,
													   ChannelInterface::CHANNEL_INTERFACE_RECEIVE_KEY);
}

void ChannelInterface::disconnectChannel(std::string hostName)
{
	channel::ChannelMessage message;

	ipc::MessageQueue<channel::ChannelMessage> queue(
			ipc::MessageQueue<channel::ChannelMessage>::get(
					ChannelInterface::CHANNEL_INTERFACE_PATH+hostName,
					ChannelInterface::CHANNEL_INTERFACE_SEND_KEY));

	message.destino = 0;
	message.tipo = channel::CHANNEL_END_MSG;
	message.size = hostName.size();
	strcpy(message.payload,hostName.c_str());

	queue.put(1,message);

	usleep(10000);
}

void ChannelInterface::destroy(std::string hostName)
{
	ipc::MessageQueue<channel::ChannelMessage>::destroy(ChannelInterface::CHANNEL_INTERFACE_PATH+hostName,
			ChannelInterface::CHANNEL_INTERFACE_SEND_KEY);
	ipc::MessageQueue<channel::ChannelMessage>::destroy(ChannelInterface::CHANNEL_INTERFACE_PATH+hostName,
			ChannelInterface::CHANNEL_INTERFACE_RECEIVE_KEY);
}

} /* namespace channel */
